package com.qanda.data.adapters;

import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.qanda.data.entities.*;
import com.qanda.http.*;


public class TemeAdapter extends DataAdapter {
	
	public TemeAdapter()
	{
		this.dispatcher=new HttpDispatcher();
	}
	
	public Vector<Anketa> najnovijeAnkete()
	{
		String httpResponse;
		try {
			httpResponse = dispatcher.getHttpResponse_GET(
					pageBase+"najnovije_ankete.php");
			return this.parseAnkete(httpResponse);
		}
		catch (Exception e) {
			return new Vector<Anketa>();
		}
	}
	public Vector<Pitanje> najnovijaPitanja()
	{
		String httpResponse;
		try {
			httpResponse = dispatcher.getHttpResponse_GET(
					pageBase+"najnovija_pitanja.php");
			return this.parsePitanja(httpResponse);
		}
		catch (Exception e) {
			return new Vector<Pitanje>();
		}
	}
	
	public boolean dodajPitanje(Pitanje p)
	{
		Vector<String> postParameters=new Vector<String>();
		postParameters.add("idKorisnik="+p.getIdKorisnik());
		postParameters.add("idKategorija="+p.getIdKategorija());
		postParameters.add("naslov="+p.getNaslov());
		postParameters.add("tekst="+p.getTekst());

		String httpResponse;
		try {
			httpResponse = dispatcher.getHttpResponse_POST(pageBase+"dodaj_pitanje.php", postParameters);
		} catch (Exception e) {
			return false;
		}	
		return httpResponse.equals("1");
	}
	
	public boolean dodajAnketu(Anketa a)
	{
		//nije zavrseno-fale pitanja i stavke, tj. mnogo toga!!
		Vector<String> postParameters=new Vector<String>();
		postParameters.add("idKorisnik="+a.getIdKorisnik());
		postParameters.add("naslov="+a.getNaslov());
		

		String httpResponse;
		try {
			httpResponse = dispatcher.getHttpResponse_POST(pageBase+"dodaj_pitanje.php", postParameters);
		} catch (Exception e) {
			return false;
		}	
		return httpResponse.equals("1");
	}
	
	public boolean dodajAnketu()
	{
		return true;
	}
	
	private Vector<Pitanje> parsePitanja(String httpJSONObject)throws Exception
	{
		Vector<Pitanje> pitanja = new Vector<Pitanje>();
		JSONArray pitanjaArray = new JSONArray(httpJSONObject);
		for(int i=0;i<pitanjaArray.length();i++)
		{
			JSONObject pitanje = pitanjaArray.getJSONObject(i);
			Pitanje p = new Pitanje();
			p.setIdKorisnik(pitanje.getInt("idKorisnik"));
			p.setIdTema(pitanje.getInt("idTema"));
			p.setNaslov(pitanje.getString("naslov"));
			p.setOcena((float)pitanje.getDouble("ocena"));
			p.setStatus(pitanje.getString("status"));
			p.setTekst(pitanje.getString("tekst"));
			p.setIdKategorija(pitanje.getInt("idKategorija"));
			p.setVaziOdgDo(pitanje.getInt("vaziOdgDo"));
			//a.setVaziVreme) videti kako se parsira datum iz stringa!"!!
			
			pitanja.add(p);
		}
		
		return pitanja;
	}
	
	private Vector<Anketa> parseAnkete(String httpJSONObject)throws Exception
	{
		Vector<Anketa> ankete = new Vector<Anketa>();
		JSONArray anketeArray = new JSONArray(httpJSONObject);
		for(int i=0;i<anketeArray.length();i++)
		{
			JSONObject anketa = anketeArray.getJSONObject(i);
			Anketa a=new Anketa();
			a.setIdKorisnik(anketa.getInt("idKorisnik"));
			a.setIdTema(anketa.getInt("idTema"));
			a.setNaslov(anketa.getString("naslov"));
			a.setOcena((float)anketa.getDouble("ocena"));
			a.setStatus(anketa.getString("status"));
			a.setTip(anketa.getString("tip"));
			a.setVaziOdgDo(anketa.getInt("vaziOdgDo"));
			//a.setVaziVreme) videti kako se parsira datum iz stringa!"!!
			
			ankete.add(a);
		}
		
		return ankete;
	}
}
